use std::process::Command;

#[test]
fn test_help() {
    let output = Command::new("target/debug/image-tiler")
        .args(["--help"])
        .output()
        .expect("image-tiler should be executable");

    let stdout = String::from_utf8(output.stdout).expect("output of image-tiler should be UTF-8");
    assert!(output.status.success());
    let expected_stdout = "\
Program to generate a larger image made up of different image tiles of the same size

Usage: image-tiler [OPTIONS] <ROWS> <COLS>

Arguments:
  <ROWS>
          Number of rows

  <COLS>
          Number of columns

Options:
  -b, --border <CONNECTOR_NAME>
          Optional connector name that tiles must exhibit towards the border of the image; if this
          option is omitted, bordering tiles may have any connection towards the border

  -c, --config <PATH>
          Optional path to a tile configuration file; overrides any configuration file in the tile
          directory

  -d, --tile-directory <PATH>
          Optional path to a tile directory; if omitted, assume the current working directory.
          
          The directory may contain a tile configuration file named “config.yaml”.

  -m, --missing-tile-policy <MISSING_TILE_POLICY>
          Optional policy name that specifies how to behave in case there is no tile with a required
          connectors list.
          
          MISSING_TILE_POLICY may have one of the following values:
          
          avoid:
           Try to avoid leaving empty positions in the tile grid. This may take a considerable
           amount of time and memory to finish due to backtracking. The run may terminate
           unsuccessfully in case there is no solution.
          
          exit:
           Exit immediately if there is no fitting tile; this may be useful for debugging tile
           configurations.
          
          [default: avoid]
          [possible values: avoid, exit]

  -n, --no-config
          Do not use any user defined tile configuration; Instead, assume all tiles have the
          connectors { north: “c”, east: “c”, south: “c”, west: “c” } and a weight of 1

  -o, --output <PATH>
          Path to the output file
          
          [default: output/tiled]

  -v, --vacancy-policy <VACANCY_POLICY>
          Optional policy name that specifies how to handle vacant areas.
          
          VACANCY_POLICY may have one of the following values:
          
          avoid:
           Try to avoid leaving vacant areas in the tile grid. This may take a considerable amount
           of time and memory to finish due to backtracking. The run may terminate unsuccessfully in
           case there is no solution.
          
          avoid-strict:
           Same as avoid, but searches the entire problem space for a valid solution, if necessary,
           which might be much slower.
          
          ignore:
           Ignore and vacant areas in the tile grid untouched.
          
          [default: avoid]
          [possible values: avoid, avoid-strict, ignore]

      --tile-selection-policy <TILE_SELECTION_POLICY>
          Optional policy name that specifies how to select tiles in case multiple fit into a slot.
          
          TILE_SELECTION_POLICY may have one of the following values:
          
          random:
           Select a fitting tile at random using the provided weights. The tile weights need to be
           non-negative and their sum must not be zero.
          
          priority:
           Select the tile with the greatest weight that fits.
          
          [default: random]
          [possible values: priority, random]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version
";
    assert_eq!(stdout, expected_stdout);
    assert_eq!(output.stderr.len(), 0);
}

#[test]
fn check_that_parameters_config_and_no_config_are_not_passed_together() {
    let output = Command::new("target/debug/image-tiler")
        .args(["-c", "file.yaml", "-n"])
        .output()
        .expect("failed to execute command");

    let stderr = String::from_utf8(output.stderr).expect("output of image-tiler should be UTF-8");
    assert_eq!(output.status.code(), Some(2));
    assert_eq!(output.stdout.len(), 0);
    let expected_stderr = "\
error: the argument '--config <PATH>' cannot be used with '--no-config'

Usage: image-tiler --config <PATH> <ROWS> <COLS>

For more information, try '--help'.
";
    assert_eq!(stderr, expected_stderr);
}
